﻿using Dapper;
using OMG.Fill.Potato.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Potato.Services
{
    /// <summary>
    /// 马铃薯全年生产情况统计表通用操作类
    /// </summary>
    public class PotYearProducts
    {
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(List<PotYearProductEntity> list) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_PotYearProduct";
            strSql +="(";
            strSql +="Id,";
            strSql +="ReportId,";
            strSql += "Pvariety,";
            strSql +="YArea,";
            strSql +="YAvgMuYield,";
            strSql +="YYield,";
            strSql +="YOutput,";
            strSql +="SArea,";
            strSql +="SAvgMuYield,";
            strSql +="SYield,";
            strSql +="SOutput,";
            strSql +="AArea,";
            strSql +="AAvgMuYield,";
            strSql +="AYield,";
            strSql +="AOutput,";
            strSql +="WArea,";
            strSql +="WAvgMuYield,";
            strSql +="WYield,";
            strSql +="WOutput)";
            strSql +=" values(";
            strSql +="@Id,";
            strSql +="@ReportId,";
            strSql += "@Pvariety,";
            strSql +="@YArea,";
            strSql +="@YAvgMuYield,";
            strSql +="@YYield,";
            strSql +="@YOutput,";
            strSql +="@SArea,";
            strSql +="@SAvgMuYield,";
            strSql +="@SYield,";
            strSql +="@SOutput,";
            strSql +="@AArea,";
            strSql +="@AAvgMuYield,";
            strSql +="@AYield,";
            strSql +="@AOutput,";
            strSql +="@WArea,";
            strSql +="@WAvgMuYield,";
            strSql +="@WYield,";
            strSql +="@WOutput)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    var trans = dbCon.BeginTransaction();

                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].Id = CustomFun.GetGuid();
                    }

                    string strDel = "delete from dbo.T_PotYearProduct where ReportId=@ReportId";
                    int sst =  dbCon.Execute(strDel, new { ReportId = list[0].ReportId }, trans);
                    int result = dbCon.Execute(strSql, list, trans);
                    string strRep = "update dbo.T_Report set Status='2',UpdateTime=@UpdateTime where Id=@ReportId";
                    dbCon.Execute(strRep, new { ReportId = list[0].ReportId, UpdateTime = DateTime.Now }, trans);
                    //提交事务
                    trans.Commit();
                    obj.model = result;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "更新成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增马铃薯全年生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(PotYearProductEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.T_PotYearProduct set ";
            strSql+="ReportId = @ReportId,";
            strSql+="YArea = @YArea,";
            strSql+="YAvgMuYield = @YAvgMuYield,";
            strSql+="YYield = @YYield,";
            strSql+="YOutput = @YOutput,";
            strSql+="SArea = @SArea,";
            strSql+="SAvgMuYield = @SAvgMuYield,";
            strSql+="SYield = @SYield,";
            strSql+="SOutput = @SOutput,";
            strSql+="AArea = @AArea,";
            strSql+="AAvgMuYield = @AAvgMuYield,";
            strSql+="AYield = @AYield,";
            strSql+="AOutput = @AOutput,";
            strSql+="WArea = @WArea,";
            strSql+="WAvgMuYield = @WAvgMuYield,";
            strSql+="WYield = @WYield,";
            strSql+="WOutput = @WOutput";
            strSql+=" where Id = @Id";
           try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        string strRep = "update dbo.T_Report set Status='2',UpdateTime=@UpdateTime where Id=@ReportId";
                        dbCon.Execute(strRep, new { ReportId = model.ReportId, UpdateTime = DateTime.Now });
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "更新成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "更新失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新马铃薯全年生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_PotYearProduct where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除马铃薯全年生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_PotYearProduct where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除马铃薯全年生产情况统计表表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public virtual ResResult<List<PotYearProductEntity>> GetModel(string ReportId) {
            ReportServices reportService = new ReportServices();
            ResResult<List<PotYearProductEntity>> obj = new ResResult<List<PotYearProductEntity>>();
            PotYearProductEntity entity = new PotYearProductEntity();
            List<PotYearProductEntity> list = new List<PotYearProductEntity>();
            string strSql = @"select * from dbo.T_PotYearProduct where ReportId = @ReportId";           
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (reportService.CheckRepCount(ReportId, "T_PotYearProduct"))
                    {
                        entity.ReportId = ReportId;
                        list.Add(entity);
                    }
                    else
                    {
                        list = dbCon.Query<PotYearProductEntity>(strSql, new { ReportId = ReportId }).ToList();
                    }
                    
                    obj.model = list;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取马铃薯全年生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<PotYearProductEntity>> GetList()
        {
            ResResult<List<PotYearProductEntity>> obj = new ResResult<List<PotYearProductEntity>>();
            string strSql = @"select * from dbo.T_PotYearProduct";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<PotYearProductEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新马铃薯全年生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
    }
}